package com.inspur.myapplication.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.inspur.myapplication.R;
import com.inspur.myapplication.dao.OrderDao;
import com.inspur.myapplication.entity.UserBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.HashMap;

public class OrderActivity extends AppCompatActivity {
    private ArrayList arrayList;
    private OrderDao orderDao;
    private ImageLoader imageLoader;
    private boolean edit;
    private MyAdpater myAdpater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_order);
        orderDao=new OrderDao(this);
        //查询数据
        arrayList=orderDao.queryOder(UserBean.currentUserName);

        imageLoader=ImageLoader.getInstance();
        ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this).build();
        imageLoader.init(config);

        //可扩展列表，设置数据
        ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.order_exp_listview);
        myAdpater=new MyAdpater();
        expandableListView.setAdapter(myAdpater);
    }

    //自定义数据适配器
    private  class  MyAdpater extends BaseExpandableListAdapter{

        @Override
        public int getGroupCount() {//得到一级数据的数目
            return arrayList.size();
        }

        @Override
        public int getChildrenCount(int i) {//得到二级数据的数目
            HashMap hashMap=(HashMap) arrayList.get(i);
            String orderId=(String)hashMap.get("orderId");
            ArrayList arrayList1=orderDao.queryOderDetail(orderId);
            return arrayList1.size();
        }

        @Override
        public Object getGroup(int i) {//得到一级数据的对象
            return arrayList.get(i);
        }

        @Override
        public Object getChild(int i, int i1) {//得到一级数据的对象,两个参数分别代表：订单的索引和明细的索引
            HashMap hashMap=(HashMap) arrayList.get(i);
            String orderId=(String)hashMap.get("orderId");
            ArrayList arrayList1=orderDao.queryOderDetail(orderId);

            return arrayList1.get(i1);
        }

        @Override
        public long getGroupId(int i) {//得到一级数据的ID
            HashMap hashMap=(HashMap) arrayList.get(i);
            String orderId=(String)hashMap.get("orderId");
            return Long.parseLong(orderId);
        }

        @Override
        public long getChildId(int i, int i1) {//得到二级数据的ID
            HashMap hashMap=(HashMap)getChild(i,i1);
            String goodsId=(String)hashMap.get("goodsId");
            return Long.parseLong(goodsId);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {//得到一级数据的界面
            if(view==null){
                LayoutInflater layoutInflater=getLayoutInflater();
                view=layoutInflater.inflate(R.layout.layout_order_item,null);
            }
            HashMap hashMap=(HashMap)arrayList.get(i);
            //对view对象中的界面元素进行赋值
            TextView textView=(TextView) view.findViewById(R.id.order_item_date);
            textView.setText(hashMap.get("orderDate").toString());
            textView=(TextView) view.findViewById(R.id.order_item_money);
            textView.setText(hashMap.get("orderMoney").toString());
            Button button=(Button)view.findViewById(R.id.order_item_del);
            //判断是否处于编辑状态，处理删除按键的显示
            if(edit){
                button.setVisibility(View.VISIBLE);
            }else {
                button.setVisibility(View.INVISIBLE);
            }

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    arrayList.remove(i);//还需要调用DAO对象进行删除
                    myAdpater.notifyDataSetChanged();
                }
            });
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {//得到二级数据的界面
            if(view==null){
                LayoutInflater layoutInflater=getLayoutInflater();
                view=layoutInflater.inflate(R.layout.layout_order_detail,null);
            }
            HashMap hashMap=(HashMap)arrayList.get(i);
            String orderId=(String)hashMap.get("orderId");
            ArrayList detailList=orderDao.queryOderDetail(orderId);
            hashMap=(HashMap)detailList.get(i1);
            TextView textView=(TextView) view.findViewById(R.id.order_detail_name);
            textView.setText(hashMap.get("goodsName").toString());
            textView=(TextView) view.findViewById(R.id.order_detail_price);
            textView.setText(hashMap.get("goodsPrice").toString());
            textView=(TextView) view.findViewById(R.id.order_detail_quantity);
            textView.setText(hashMap.get("quantity").toString());
            ImageView imageView=(ImageView)view.findViewById(R.id.order_detail_iv);
            imageLoader.displayImage(hashMap.get("goodsImg").toString(),imageView);
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }
    }


    public void editClick(View view){
        edit=!edit;
        if(edit){
            ((Button)view).setText("完成");
        }else {
            ((Button)view).setText("编辑");
        }
        myAdpater.notifyDataSetChanged();
    }

}
